package com.b2c.service;

import com.b2c.mapper.CategoryMapper;
import com.mr.service.pojo.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CategoryService {
    //注入mapper
    @Autowired
    private CategoryMapper categoryMapper;

    //方法
    public List<CategoryEntity> queryCategoryByPid(Long pid){
        CategoryEntity category=new CategoryEntity();
        category.setParentId(pid);
        return categoryMapper.select(category);
    }

    public void save(CategoryEntity entity) {

        CategoryEntity entity2 = new CategoryEntity();
        entity2.setId(entity.getParentId());
        entity2 =  categoryMapper.selectByPrimaryKey(entity2);
        System.out.println(entity2);
        if (entity2.getIsParent()!=true&&entity!=null){
            entity2.setIsParent(true);
            categoryMapper.updateByPrimaryKeySelective(entity2);
        }
        categoryMapper.insert(entity);
    }


    //删除
    public void delCategoryById(Long id){

        categoryMapper.deleteByPrimaryKey(id);
    }

    public void update(CategoryEntity entity) {
        categoryMapper.updateByPrimaryKeySelective(entity);
    }

    public List<CategoryEntity> queryCategoryListByBid(Long bid){
        return categoryMapper.queryCategoryListByBid(bid);
    }

    public List<String> queryNamesByIds(List<Long> ids) {
        List<CategoryEntity> list = this.categoryMapper.selectByIdList(ids);
        List<String> names = new ArrayList<>();
        for (CategoryEntity category : list) {
            names.add(category.getName());
        }
        return names;
    }


    public List<CategoryEntity> queryCategoryByids(List<Long> ids){
        return categoryMapper.selectByIdList(ids);
    }

    public List<String> queryCateNameByIds(List<Long> ids){
        //查询多条数据
        List<CategoryEntity> list=categoryMapper.selectByIdList(ids);
        //取出单独 名称 一列 进行返回
        List<String> nameList=list.stream().map(category -> {
            return category.getName();
        }).collect(Collectors.toList());
        return  nameList;
    }


    /**
     * 前台页面分类展示
     * @return
     */

    public Map<String, Map<String, List<String>>> queryCate() {


        Map<String, Map<String, List<String>>> map = new HashMap<>();
        //根据parentId=0查询第一级分类
        CategoryEntity category1 = new CategoryEntity();
        category1.setParentId(0l);
        List<CategoryEntity> list1 = categoryMapper.select(category1);

        //遍历第一级分类
        list1.forEach(cate1 ->{
            //获取第一级的id
            Long oneId = cate1.getId();
            //根据第二级的 parentId=第一级的id 查询第二级分类
            CategoryEntity category2 = new CategoryEntity();
            category2.setParentId(oneId);
            category2.setIsParent(true);
            List<CategoryEntity> list2 = categoryMapper.select(category2);

            Map<String, List<String>> sonMap = new HashMap<>();

            //遍历第二级分类
            list2.forEach(cate2 ->{
                //获取第二级的id
                Long twoId = cate2.getId();
                List<String> threeList = categoryMapper.getName(twoId, false);
                sonMap.put(cate2.getName(),threeList);
            });

            map.put(cate1.getName(),sonMap);

        });
        return map;
    }

}
